POV-Ray : Newsgroups : povray.programming : Vectors in C++ : Re: Vectors in C++ Server Time
6 Oct 2024 13:48:01 EDT (-0400)
  Re: Vectors in C++  
From: Micha Riser
Date: 16 Dec 2002 13:40:14
Message: <3dfe1e0d@news.povray.org>
Tony[B] wrote:

> A few comments...
> 
> 1) Why do you make the array public?

The vector is a really basic class which needs to be manipulated 
"low-level" by many other classes. To allow this I made the array public. 
If I made it private I would have to add a public method to read and write 
the elements of the vector anyways. But access could be made a bit more 
elegant using the '[]' operator.

> 
> 2) Why do you put "public" in so many times? Once will do...

I think it makes it more clear. Each 'public' comes with a comment to its 
right and kinda defines a new section. This way the sections' comments can 
easily be distinguished from other comments.

> 
> 3) Why do you use a for-loop in the constructor? Is this
> 
> a[0] = a[1] = a[2] = w;
> 
> not possible, and perhaps clearer?

I think multiple assinments are rather counter-intuitive. But this was not 
the reasons I chose for-loops. The thing is that an explizit loop tells the 
compiler about the repeating structure while the other does not. The 
compiler can easily unroll the loop when it thinks that this will help 
performance or whatever. Furthermore the Intel compiler recoginzes it this 
way and can 'vectorize' it, that is use SIMD instructions.
 
> 4) Instead of "equals", "add", and "sub", wouldn't it make more sense to
> overload ==, +, and -?

I am not really a friend of overloading of + and -. They hide what is 
really going on when using them. Sometimes there is also an additional 
temporary variable created.

I think
 a += b + c;
will have to be tranlated by the compiler to
 tmp = c;
 tmp += b;
 a += tmp;
as it cannot assume that (a+b)+c is the same as a+(b+c). On the other hand
 a.add(b); a.add(c); 
does not need a temporary variable.

Thanks for your comments!

- Micha

-- 
objects.povworld.org - The POV-Ray Objects Collection
book.povworld.org    - The POV-Ray Book Project


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.